
package org.wso2.carbon.registry.custom.dia.services;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.wso2.carbon.registry.common.services.RegistryAbstractAdmin;
import org.wso2.carbon.registry.core.Registry; 
import org.wso2.carbon.registry.core.Association;
import org.wso2.carbon.registry.core.Tag;
import org.wso2.carbon.registry.custom.dia.beans.AssociationBean;
import org.wso2.carbon.registry.custom.dia.beans.DiaBean;
import org.wso2.carbon.registry.custom.dia.beans.TagBean;
import org.wso2.carbon.registry.custom.dia.util.DiaUtil;

public class DiaService extends RegistryAbstractAdmin{

    public DiaBean getDiaBean(String path) throws Exception {
        return DiaUtil.getDiaBean(path);       
    }

    public void addAssociation(String sPath, String dPath, String type) {
        try {
            Registry registry = getRootRegistry();
            registry.addAssociation(sPath, dPath, type);
        } catch (Exception ex) {
            Logger.getLogger(DiaService.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error in backend when adding a Association.");
        }
    }

    public AssociationBean[] getAllAssociations(String sPath){
        try {
            Registry registry = getRootRegistry();  
            Association[] associations = registry.getAllAssociations(sPath);

            AssociationBean[] assoBeans = new AssociationBean[associations.length];
            int index = 0;
            for(Association asso: associations){
                AssociationBean assoBean = new AssociationBean(asso);
                assoBeans[index++] = assoBean;
            } 
            return assoBeans;
        } catch (Exception ex) {
            Logger.getLogger(DiaService.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error in backend when getting Associationssssss.");
           return null;
        }
    }

   public void applyTag(String sPath, String tag){
        try {
            Registry registry = getRootRegistry();
            registry.applyTag(sPath, tag);
        } catch (Exception ex) {
            Logger.getLogger(DiaService.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Error in backend when adding a tag.");
        }
   }

   public TagBean[] getTags(String sPath){
        try {
            Registry registry = getRootRegistry();
            Tag[] tags = registry.getTags(sPath);

            TagBean[] tagBeans = new TagBean[tags.length];
            int index = 0;
            for(Tag tag: tags){
                TagBean tagBean = new TagBean(tag);
                tagBeans[index++] = tagBean;
            }

            return tagBeans;
        } catch (Exception ex) {
            Logger.getLogger(DiaService.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Error in backend when getting tags.");
            return null;
        }
   }

}

